#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 305;
int f[maxn][maxn],data[maxn],bro[maxn],son[maxn],n,m;

int init(){
	cin>>n>>m;
	data[n+1] = 0;
	bro[n+1] = 0;
	son[n+1] = 0;
	int x,y;
	for(int i=1;i<=n;i++){
		cin>>x>>y;
		if(x==0)x=n+1;
		data[i]=y;
		bro[i]=son[x];
		son[x]=i;
	}
}

void dfs(int s,int num){
	if(f[s][num]>=0) return;
	if(s==0||num==0){
		f[s][num]=0;
		return;
	}
	
	dfs(bro[s],num);
	f[s][num] = f[bro[s]][num];
	
	for(int k=0;k<num;k++){
		dfs(bro[s],k);
		dfs(son[s],num-k-1);
		f[s][num] = max(f[s][num],f[bro[s]][k]+f[son[s]][num-k-1]+data[s]);
	}
}

int main(){
	init();
	memset(f,-1,sizeof f);
	dfs(son[n+1],m);
	cout<<f[son[n+1]][m]<<endl;
}